﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WF_Office_Verwaltung
{
    public partial class frm_Suche : Form
    {
        nachrichten_klasse aNachrichtenKlasse = new nachrichten_klasse();
        helfer_klasse aHelferKlasse = new helfer_klasse();
        public frm_Suche()
        {
            InitializeComponent();
        }
        private void db_suche_parametrisiert()
        {
            try
            {
                using (var context = new project_entities())
                {
                    if (!cb_Rechnungs_nummer.Checked)
                    {
                        string vorname, nachname, PLZ, ort, adresse, mobil, festnetz;
                        #region query_parameter
                        #region vorname
                        if (txt_search_Vorname.Text != "")
                        {
                            vorname = txt_search_Vorname.Text;
                        }
                        else
                        {
                            vorname = null;
                        }
                        #endregion
                        #region nachname
                        if (txt_search_Nachname.Text != "")
                        {
                            nachname = txt_search_Nachname.Text;
                        }
                        else
                        {
                            nachname = null;
                        }
                        #endregion
                        #region PLZ
                        if (txt_search_PLZ.Text != "")
                        {
                            PLZ = txt_search_PLZ.Text;
                        }
                        else
                        {
                            PLZ = null;
                        }
                        #endregion
                        #region Ort
                        if (txt_search_Ort.Text != "")
                        {
                            ort = txt_search_Ort.Text;
                        }
                        else
                        {
                            ort = null;
                        }
                        #endregion
                        #region Adresse
                        if (txt_search_Adresse.Text != "")
                        {
                            adresse = txt_search_Adresse.Text;
                        }
                        else
                        {
                            adresse = null;
                        }
                        #endregion
                        #region Mobil
                        if (txt_search_Tele.Text != "")
                        {
                            mobil = txt_search_Tele.Text;
                        }
                        else
                        {
                            mobil = null;
                        }
                        #endregion
                        #region Festnetz
                        if (txt_search_Festnetz.Text != "")
                        {
                            festnetz = txt_search_Festnetz.Text;
                        }
                        else
                        {
                            festnetz = null;
                        }
                        #endregion
                        #region KundenNR
                        int? id_kunde = null;

                        if (txt_search_KundenNr.Text != "")
                        {
                            // TODO : sp um Kunden ID erweitern und mit dieser Aufrufen
                            string nKundenId = txt_search_KundenNr.Text;
                            id_kunde = int.Parse(nKundenId);
                        }
                        else
                        {
                            id_kunde = null;
                        }
                        #endregion
                        #endregion

                        var search_result = context.sp_search_Kunde(vorname, nachname, PLZ, ort, adresse, mobil, festnetz, id_kunde).ToList();
                        dgv_search_result.DataSource = search_result;
                        dgv_search_result.Columns[0].Width = 140;
                        dgv_search_result.Columns[1].Width = 160;
                        dgv_search_result.Columns[2].Width = 60;
                        dgv_search_result.Columns[4].Width = 175;
                        dgv_search_result.Columns[4].DisplayIndex = 2;
                        dgv_search_result.Columns[7].DisplayIndex = 0;
                        dgv_search_result.Columns[7].HeaderText = "Kunden Nr.";
                        dgv_search_result.Columns[7].Width = 130;

                        btn_serach_Refresh.Visible = true;
                    }
                    else
                    {
                        var q_rechnung = context.sp_search_Rechnungs_Nummer(txt_search_Rechnung.Text).ToList();
                        dgv_search_result.DataSource = q_rechnung;
                        btn_serach_Refresh.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                aNachrichtenKlasse.ErrorMessage("Suche : btn_suchen_Click\n", ex);
            }
        }
        private void btn_suchen_Click(object sender, EventArgs e)
        {
            db_suche_parametrisiert();
        }
        private void btn_serach_Refresh_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult aDialogResult = aNachrichtenKlasse.Warnmessage("Soll das Ergebnis wirklich zurückgesetzt werde?");

                if (aDialogResult == DialogResult.OK)
                {
                    txt_search_Vorname.Text = "";
                    txt_search_Nachname.Text = "";
                    txt_search_PLZ.Text = "";
                    txt_search_Ort.Text = "";
                    txt_search_Adresse.Text = "";
                    txt_search_Tele.Text = "";
                    txt_search_Festnetz.Text = "";
                    dgv_search_result.DataSource = null;
                    btn_serach_Refresh.Visible = false;
                }

            }
            catch (Exception ex)
            {
                aNachrichtenKlasse.ErrorMessage(ex);
            }

        }
        private void search_form_checkedChange(object sender, EventArgs e)
        {
            try
            {
                if (sender == cb_Rechnungs_nummer)
                {
                    if (cb_Rechnungs_nummer.Checked)
                    {
                        if (txt_search_Vorname.Text != "" || txt_search_Nachname.Text != "" || txt_search_PLZ.Text != "" || txt_search_Ort.Text != "" || txt_search_Adresse.Text != "" || txt_search_Tele.Text != "" || txt_search_Festnetz.Text != "")
                        {
                            zeige_Mbox();
                        }
                        else
                        {
                            aktiviere_RechnungsNr_Suche();
                        }
                    }
                    else
                    {
                        txt_search_Rechnung.Enabled = false;
                        txt_search_Rechnung.Text = "";
                        txt_search_Vorname.Enabled = true;
                        txt_search_Nachname.Enabled = true;
                        txt_search_PLZ.Enabled = true;
                        txt_search_Ort.Enabled = true;
                        txt_search_Adresse.Enabled = true;
                        txt_search_Tele.Enabled = true;
                        txt_search_Festnetz.Enabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                aNachrichtenKlasse.ErrorMessage(ex);
            }

        }
        private void zeige_Mbox()
        {
            try
            {
                DialogResult diaResult = aNachrichtenKlasse.Warnmessage("Alle vorasugefüllten Textfelder werden geleert. Wollen Sie das wirklich?");

                if (diaResult == DialogResult.OK)
                {
                    aktiviere_RechnungsNr_Suche();
                }
                else if (diaResult == DialogResult.Cancel)
                {
                    cb_Rechnungs_nummer.Checked = false;
                }
            }
            catch (Exception ex)
            {
                aNachrichtenKlasse.ErrorMessage(ex);


            }
        }
        private void aktiviere_RechnungsNr_Suche()
        {
            try
            {
                txt_search_Rechnung.Enabled = true;
                txt_search_Vorname.Text = "";
                txt_search_Nachname.Text = "";
                txt_search_PLZ.Text = "";
                txt_search_Ort.Text = "";
                txt_search_Adresse.Text = "";
                txt_search_Tele.Text = "";
                txt_search_Festnetz.Text = "";

                txt_search_Vorname.Enabled = false;
                txt_search_Nachname.Enabled = false;
                txt_search_PLZ.Enabled = false;
                txt_search_Ort.Enabled = false;
                txt_search_Adresse.Enabled = false;
                txt_search_Tele.Enabled = false;
                txt_search_Festnetz.Enabled = false;
            }
            catch (Exception ex)
            {
                aNachrichtenKlasse.ErrorMessage(ex);
            }
        }

        private void dgv_search_result_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int cur_index = dgv_search_result.CurrentRow.Index;

                int nQueryedKunde = int.Parse(dgv_search_result.Rows[dgv_search_result.CurrentRow.Index].Cells["ID_Kunde"].Value.ToString());
                frm_Kundenformular kundenformular = new frm_Kundenformular(nQueryedKunde);
                kundenformular.Show();
                //tabKundenverwaltung.SelectedIndex = 1;
            }
            catch (Exception ex)
            {
                aNachrichtenKlasse.ErrorMessage(ex);
            }
        }

        private void txt_search_KundenNr_onEnterKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                db_suche_parametrisiert();
            }
        }


    }
}
